package com.closer.service.biz.ai.core.saver;


import cn.hutool.core.util.StrUtil;
import com.closer.service.biz.ai.dto.MultiFileCodeResult;
import com.closer.service.biz.ai.enums.CodeGenTypeEnum;
import com.closer.service.support.exception.BusinessException;
import com.closer.service.support.exception.ErrorCode;

/**
 * 多文件代码保存模板
 */
public class MultiFileCodeFileSaverTemplate extends CodeFileSaverTemplate<MultiFileCodeResult> {

    @Override
    protected CodeGenTypeEnum getCodeGenType() {
        return CodeGenTypeEnum.MULTI_FILE;
    }

    @Override
    protected void saveFiles(MultiFileCodeResult result, String dirPath) {
        //1. 保存HTML代码
        writeToFile(dirPath, "index.html", result.getHtmlCode());
        //2. 保存CSS代码
        writeToFile(dirPath, "style.css", result.getCssCode());
        //3. 保存JS代码
        writeToFile(dirPath, "script.js", result.getJsCode());
    }

    @Override
    protected void validateInput(MultiFileCodeResult result) {
        super.validateInput(result);
        if(StrUtil.isBlank(result.getHtmlCode())){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "HTML代码不能为空");
        }
    }

}
